package com.lenovo.leos.filebrowser.netdisk;

import android.app.Activity;
import android.app.Activity.OnTitleIconClickListener;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.DirDefines;

public class LenovoUtilActivity extends Activity implements OnClickListener,
		OnTouchListener, OnTitleIconClickListener {

	private static final String TAG = "LenovoUtilActivity";
	private static final boolean TAG_FLAG = true;
	private String projectState;
	private String downloadState;
	private String uploadState;
	private AlertDialog dialog;
	private TextView sdcardText;
	private String OPEN_SAVE = "android.intent.action.OPEN_SAVE_DIALOG";
	private static final int MODE_SELECT_PATH = 1;
	private static final int LENOVO_MODE_SAVE_FILE = 3;
	private boolean mReceiver = false;
	private String sdCardtatus = Environment.getExternalStorageState();
	private LinearLayout layout, phoneLayout, sdcardLayout, lenovoLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		IntentFilter commandFilters = new IntentFilter();
		commandFilters.addAction(Intent.ACTION_MEDIA_MOUNTED);
		commandFilters.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
		commandFilters.addAction(Intent.ACTION_MEDIA_REMOVED);
		commandFilters.addAction(Intent.ACTION_MEDIA_SHARED);
		commandFilters.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
		commandFilters.addAction(Intent.ACTION_MEDIA_CHECKING);
		commandFilters.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
		commandFilters.addAction(Intent.ACTION_MEDIA_NOFS);
		commandFilters.addDataScheme("file");
		registerReceiver(sdCardIntentReceiver, commandFilters);
		mReceiver = true;
		projectState = getIntent().getStringExtra("projectState");
		downloadState = getIntent().getStringExtra("downloadState");
		uploadState = getIntent().getStringExtra("uploadState");

		layout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.dir_dialog_list, null);
		sdcardText = (TextView) layout.findViewById(R.id.sdcardText);
		phoneLayout = (LinearLayout) layout.findViewById(R.id.phoneLayout);
		sdcardLayout = (LinearLayout) layout.findViewById(R.id.sdcardLayout);
		lenovoLayout = (LinearLayout) layout.findViewById(R.id.lenovoLayout);
		phoneLayout.setOnTouchListener(this);
		sdcardLayout.setOnTouchListener(this);
		lenovoLayout.setOnTouchListener(this);
		phoneLayout.setOnClickListener(LenovoUtilActivity.this);
		lenovoLayout.setOnClickListener(LenovoUtilActivity.this);
		//modify by gaoge ,Environment.MEDIA_NO_INSERT can't complie suc
//		if (sdCardtatus.equals(Environment.MEDIA_NO_INSERT)
//				|| sdCardtatus.equals(Environment.MEDIA_SHARED)
//				|| sdCardtatus.equals(Environment.MEDIA_UNMOUNTED)
//				|| sdCardtatus.equals(Environment.MEDIA_CHECKING)) {
		
		if ( sdCardtatus.equals(Environment.MEDIA_SHARED)
				|| sdCardtatus.equals(Environment.MEDIA_UNMOUNTED)
				|| sdCardtatus.equals(Environment.MEDIA_CHECKING)) {
			if (projectState.equals(DirDefines.mDir1)) {
				sdcardText.setTextColor(android.graphics.Color.GRAY);
				sdcardLayout.setEnabled(false);
			} else if (projectState.equals(DirDefines.mDir3)) {
				sdcardLayout.setVisibility(View.GONE);
			}
		} else {
			if (projectState.equals(DirDefines.mDir1)) {
				sdcardText.setTextColor(android.graphics.Color.WHITE);
				sdcardLayout.setEnabled(true);
				sdcardLayout.setOnClickListener(this);
			} else if (projectState.equals(DirDefines.mDir3)) {
				sdcardLayout.setVisibility(View.GONE);
			}
		}

		createDialog();
	}

	public void createDialog() {

		dialog = new AlertDialog.Builder(LenovoUtilActivity.this)
				.setTitle(R.string.switching_directory)
				.setView(layout)
				.setPositiveButton(R.string.alert_dialog_cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								finish();
							}
						}).show();
	}

	public void loginDialog() {
		dialog.cancel();
		LinearLayout loginLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.lenovo_login, null);
		EditText lenovoUser = (EditText) loginLayout
				.findViewById(R.id.lenovoUser);
		EditText lenovoPwd = (EditText) loginLayout
				.findViewById(R.id.lenovoPwd);
		Button retrievePwdBtn = (Button) loginLayout
				.findViewById(R.id.retrievePwdBtn);
		retrievePwdBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(LenovoUtilActivity.this, "找回密码",
						Toast.LENGTH_SHORT).show();
			}
		});
		new AlertDialog.Builder(LenovoUtilActivity.this)
				.setTitle(R.string.login_title)
				.setView(loginLayout)
				.setPositiveButton(R.string.lenovo_login,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								Toast.makeText(LenovoUtilActivity.this, "登录接口",
										Toast.LENGTH_SHORT).show();
								Intent intent = new Intent();
								intent.putExtra("projectState", projectState);
								intent.setClass(
										LenovoUtilActivity.this,
										com.lenovo.leos.filebrowser.netdisk.LenovoActivity.class);
								startActivity(intent);
								finish();
							}
						})
				.setNeutralButton(R.string.lenovo_registered,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								Toast.makeText(LenovoUtilActivity.this, "注册接口",
										Toast.LENGTH_SHORT).show();
							}
						})
				.setNegativeButton(R.string.alert_dialog_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								finish();
							}
						}).show().setOnDismissListener(new OnDismissListener() {
					public void onDismiss(DialogInterface dialog) {
						// TODO
					}
				});
	}

	@Override
	public void onTitleIconClick() {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mReceiver) {
			unregisterReceiver(sdCardIntentReceiver);
			Log.i(TAG, "*********OnDestroy unregisterReceiver *********");
		}
		finish();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (TAG_FLAG) {
			Log.d(TAG, "projectState=================" + projectState);
			Log.d(TAG, "downloadState=================" + downloadState);
			Log.d(TAG, "uploadState=================" + uploadState);
		}
		if (DirDefines.mDir1.equals(projectState)) {
			switch (view.getId()) {
			case R.id.phoneLayout:
				if (downloadState != null) {
					if (downloadState.equals(DirDefines.DOWNLOAD)) {
						Intent intent = new Intent();
						intent.setAction(OPEN_SAVE);
						intent.putExtra("request_code", MODE_SELECT_PATH);
						startActivityForResult(intent, MODE_SELECT_PATH);
					}
				} else if (uploadState != null) {
					if (uploadState.equals(DirDefines.UPLOAD)) {
						Toast.makeText(LenovoUtilActivity.this, "直接跳转到手机选择页面",
								Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setAction(OPEN_SAVE);

						intent.putExtra("request_code", LENOVO_MODE_SAVE_FILE);
						intent.putExtra("FILE_FILTER",
								".jpg .doc .txt .ppt .xls .rmvb .mp3 .flv .apk .png .vcf");
						startActivityForResult(intent, LENOVO_MODE_SAVE_FILE);
						finish();
					}
				} else {
					Toast.makeText(LenovoUtilActivity.this, "访问手机接口",
							Toast.LENGTH_SHORT).show();
					Intent intent = new Intent();
					intent.putExtra("projectState", projectState);
					intent.setClass(LenovoUtilActivity.this,
							com.lenovo.leos.filebrowser.ui.FileBrowser.class);
					startActivity(intent);
					finish();
				}
				break;
			case R.id.sdcardLayout:
				if (downloadState != null) {
					if (downloadState.equals(DirDefines.DOWNLOAD)) {
						String OPEN_SAVE = "android.intent.action.OPEN_SAVE_DIALOG";
						int MODE_SELECT_PATH = 1;
						Intent intent = new Intent();
						intent.setAction(OPEN_SAVE);
						intent.putExtra("request_code", MODE_SELECT_PATH);
						startActivityForResult(intent, MODE_SELECT_PATH);
					}
				} else if (uploadState != null) {
					if (uploadState.equals(DirDefines.UPLOAD)) {
						Toast.makeText(LenovoUtilActivity.this, "直接跳转到SD卡选择页面",
								Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setAction(OPEN_SAVE);

						intent.putExtra("request_code", LENOVO_MODE_SAVE_FILE);
						intent.putExtra("FILE_FILTER",
								".jpg .doc .txt .ppt .xls .rmvb .mp3 .flv .apk .png .vcf");
						startActivityForResult(intent, LENOVO_MODE_SAVE_FILE);
						finish();
					}
				} else {
					Toast.makeText(LenovoUtilActivity.this, "SD卡",
							Toast.LENGTH_SHORT).show();
					Intent intent = new Intent();
					intent.putExtra("projectState", projectState);
					intent.setClass(LenovoUtilActivity.this,
							com.lenovo.leos.filebrowser.ui.FileBrowser.class);
					startActivity(intent);
                finish();
				}
				break;
			case R.id.lenovoLayout:
				Toast.makeText(LenovoUtilActivity.this, "联想网盘",
						Toast.LENGTH_SHORT).show();
				loginDialog();
				break;
			}
		} else if (DirDefines.mDir3.equals(projectState)) {
			switch (view.getId()) {
			case R.id.phoneLayout:
				Toast.makeText(LenovoUtilActivity.this, "访问手机接口",
						Toast.LENGTH_SHORT).show();
				Intent intent = new Intent();
				intent.putExtra("projectState", projectState);
				intent.setClass(LenovoUtilActivity.this,
						com.lenovo.leos.filebrowser.ui.FileBrowser.class);
				startActivity(intent);
				finish();
				break;
			case R.id.lenovoLayout:
				Toast.makeText(LenovoUtilActivity.this, "联想网盘",
						Toast.LENGTH_SHORT).show();
				loginDialog();
				break;

			}
		}
	}

	private BroadcastReceiver sdCardIntentReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.d(TAG, "#################### SDaction = " + action);
			if (DirDefines.mDir1.equals(projectState)) {
				if (action.equals(Intent.ACTION_MEDIA_SHARED)
						|| action.equals(Intent.ACTION_MEDIA_UNMOUNTABLE)
						|| action.equals(Intent.ACTION_MEDIA_UNMOUNTED)
						|| action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL)
						|| action.equals(Intent.ACTION_MEDIA_NOFS)
						|| action.equals(Intent.ACTION_MEDIA_CHECKING)
						|| action.equals(Intent.ACTION_MEDIA_REMOVED)) {
					sdcardText.setEnabled(false);
					sdcardText.setTextColor(android.graphics.Color.GRAY);
				} else {
					sdcardText.setEnabled(true);
					sdcardText.setTextColor(android.graphics.Color.WHITE);
					sdcardLayout.setOnClickListener(LenovoUtilActivity.this);
				}
			}
		}
	};

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			phoneLayout
					.setBackgroundResource(android.R.drawable.list_selector_background);
			sdcardLayout
					.setBackgroundResource(android.R.drawable.list_selector_background);
			lenovoLayout
					.setBackgroundResource(android.R.drawable.list_selector_background);
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			phoneLayout.setBackgroundColor(0);
			sdcardLayout.setBackgroundColor(0);
			lenovoLayout.setBackgroundColor(0);
		} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
			phoneLayout.setBackgroundColor(0);
			sdcardLayout.setBackgroundColor(0);
			lenovoLayout.setBackgroundColor(0);
		}
		return false;
	}

}
